package com.misty.blog.controller.fileController;

import com.misty.api.service.FileService;
import com.misty.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("files")
public class FileController {

//    @Value("${server.port}")
//    private String port;
//
//    @Value("${server.ip}")
//    private String ip;

    @Autowired
    private FileService fileService;


    @CrossOrigin
    @PostMapping("upload")
    @PreAuthorize("hasAuthority('user')")
    public Result<?> upload(MultipartFile file) throws IOException{
        return fileService.upload(file);
    }

    @GetMapping("/{flag}")
    public void getFiles(@PathVariable String flag, HttpServletResponse response) {
        //文件下载
        fileService.getFiles(flag,response);
    }
}
